import asyncio
import re

import aiohttp
import requests
from lxml import etree
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",

}
# url = 'https://books.toscrape.com/'
# res = requests.get(url, headers=headers)
# html = etree.HTML(res.text)
# items = html.xpath('//ol[@class="row"]/li')
# print(len(items))
# for item in items:
#     title = item.xpath('.//h3/a/@title')[0]
#     price = item.xpath('.//p[@class="price_color"]/text()')[0]
#     href = item.xpath('.//h3/a/@href')[0]
#     print(title, price, href)

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession(headers =  headers) as session:
        urls = (f'http://books.toscrape.com/catalogue/page-{page}.html'for page in range(50))
        tasks = [fetch(session, url) for url in urls]
        htmls = await asyncio.gather(*tasks)
        for html in htmls:
            html = etree.HTML(html)
            items = html.xpath('//ol[@class="row"]/li')
            for item in items:
                title = item.xpath('.//h3/a/@title')[0]
                price = item.xpath('.//p[@class="price_color"]/text()')[0]
                href = item.xpath('.//h3/a/@href')[0]
                print(f'书名:{title},价格: {price},连接: https://books.toscrape.com/catalogue/{href}')

asyncio.run(main())

